package com.bp.hearthealth.pulse.lnapps.bp_ui.bp_activity.bp_heart.bp_guide

import android.content.Intent
import android.view.Gravity
import android.view.View
import com.bp.hearthealth.pulse.lnapps.appsflyer.AppsFlyerUtils
import com.bp.hearthealth.pulse.lnapps.base.BpBaseActivity
import com.bp.hearthealth.pulse.lnapps.bp_extension.LogUtil
import com.bp.hearthealth.pulse.lnapps.databinding.ActivityHeartrateGuideBgBinding
import com.bp.hearthealth.pulse.lnapps.bp_ui.bp_activity.bp_heart.BpHeartRateViewModel
import com.bp.hearthealth.pulse.lnapps.bp_ui.bp_activity.bp_heart.bp_detect.BpHeartRateActivity
import com.bp.hearthealth.pulse.lnapps.bp_ui.bp_dialog.BpGuideCamarePopup
import com.bp.hearthealth.pulse.lnapps.bp_utils.Constants
import com.bp.hearthealth.pulse.lnapps.bp_utils.DoubleClickListener
import com.bp.hearthealth.pulse.lnapps.bp_utils.SpUtils
import com.gyf.immersionbar.ImmersionBar
import com.hjq.permissions.OnPermissionCallback
import com.hjq.permissions.Permission
import com.hjq.permissions.XXPermissions
import org.koin.androidx.viewmodel.ext.android.viewModel

class BpHeartRateGuideActivity : BpBaseActivity<ActivityHeartrateGuideBgBinding>() {

    private lateinit var guideCamarePopup: BpGuideCamarePopup
    private val viewModel: BpHeartRateViewModel by viewModel()

    override fun initListener() {
        super.initListener()
        LogUtil.i("启动: BpHeartRateGuideActivity")
        //how to measure页展示  hr_how_measure_show
        AppsFlyerUtils.trackEvent("hr_how_measure_show")
        viewModel.updateIsFirstGuide()
        binding.tvStart.setOnClickListener(object : DoubleClickListener() {
            override fun onNoDoubleClick(v: View?) {
                if (XXPermissions.isGranted(this@BpHeartRateGuideActivity, Permission.CAMERA)) {
                    viewModel.updateAuthorizePermission()
                    val intent = Intent()
                    intent.setClass(this@BpHeartRateGuideActivity, BpHeartRateActivity::class.java);
                    startActivity(intent)
                    finish()
                } else {
                    checkCameraPermission()
                }
            }
        })
    }

    private fun checkCameraPermission() {
        XXPermissions.with(this).permission(Permission.CAMERA)
            .request(object : OnPermissionCallback {
                override fun onGranted(permissions: List<String>, allGranted: Boolean) {
                    viewModel.updateAuthorizePermission()
//                    startSplashActivity()
                    startActivity(Intent(activity!!, BpHeartRateActivity::class.java))
                    finish()
                }

                override fun onDenied(permissions: List<String>, doNotAskAgain: Boolean) {
                    if (!SpUtils.getBoolean(Constants.DENIED)) {
                        LogUtil.i("权限被拒，开始埋点")
                        //埋点
                        //相机权限弹窗-第一次拒绝授权
                        AppsFlyerUtils.trackEvent("hr_camera_not_permit")
                        SpUtils.saveBoolean(Constants.DENIED,true)
                    }
                    if (doNotAskAgain) {
                        guideCamarePopup = BpGuideCamarePopup(this@BpHeartRateGuideActivity)
                        guideCamarePopup.showAtLocation(
                            binding.constraintLayout, Gravity.CENTER, 0, 0
                        )
                        guideCamarePopup.setOnGotItListener(object :
                            BpGuideCamarePopup.OnGotItListener {
                            override fun gotIt() {
                                XXPermissions.startPermissionActivity(
                                    this@BpHeartRateGuideActivity, permissions
                                )
                            }
                        })
                    } else {
                        checkCameraPermission()
                    }
                }
            })
    }

    override fun initImmersionBar() {
        super.initImmersionBar()
        binding.includeHeartrateGuide.apply {
            ImmersionBar.with(this@BpHeartRateGuideActivity).titleBar(toolbar).init()
//            tvBack.text = getString(R.string._heart_rate_title_str)
            tvBack.setOnClickListener {
                finish()
            }
        }
    }
}